package com.egene.tw.ui.signs;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.Shader;
import android.util.AttributeSet;
import android.view.View;


public class PowerCenterView extends View {


    private float mPercent = 0;
    private float mStrokeWidth;
    private float mStartAngle = 270;
    private int mBgColor = 0xff444546;

    private int mFgColorStart = 0xffe8502e;
    private int mFgColorEnd = 0xfff2903c;

    private LinearGradient mShader;
    private Context mContext;
    private RectF mOval;
    private RectF mOval1;
    private RectF mOval2;
    private Paint mPaint;


    public PowerCenterView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.mContext = context;

        init();
    }

    private void init() {
        mStrokeWidth = dp2px(17);

        mPaint = new Paint();
        mPaint.setAntiAlias(true);
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setStrokeWidth(mStrokeWidth);
        mPaint.setStrokeCap(Paint.Cap.ROUND);
    }

    private int dp2px(float dp) {
        return (int) (mContext.getResources().getDisplayMetrics().density * dp + 0.5f);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        mPaint.setStyle(Paint.Style.FILL);
        mPaint.setShader(null);
        mPaint.setColor(0xffe3e5e5);
        canvas.drawArc(mOval, 0, 360, false, mPaint);

        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setStrokeWidth(mStrokeWidth);
        mPaint.setShader(null);
        mPaint.setColor(mBgColor);
        canvas.drawArc(mOval, 0, 360, false, mPaint);

        mPaint.setShader(mShader);
        canvas.drawArc(mOval, mStartAngle, mPercent * 3.6f, false, mPaint);

        mPaint.setStrokeWidth(mStrokeWidth);
        canvas.drawArc(mOval1, 0, 360, true, mPaint);

        mPaint.setShader(null);
        mPaint.setStyle(Paint.Style.FILL);
        mPaint.setColor(0xffffffff);
        mPaint.setStrokeWidth(mStrokeWidth);
        canvas.drawArc(mOval2, 0, 360, true, mPaint);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);

        updateOval();

        mShader = new LinearGradient(mOval.left, mOval.top,
                mOval.left, mOval.bottom, mFgColorStart, mFgColorEnd, Shader.TileMode.MIRROR);
    }

    public float getPercent() {
        return mPercent;
    }

    public void setPercent(float mPercent) {
        this.mPercent = mPercent;
        refreshTheLayout();
    }

    private void updateOval() {
        int xp = getPaddingLeft() + getPaddingRight();
        int yp = getPaddingBottom() + getPaddingTop();
        mOval = new RectF(getPaddingLeft() + mStrokeWidth, getPaddingTop() + mStrokeWidth,
                getPaddingLeft() + (getWidth() - xp) - mStrokeWidth,
                getPaddingTop() + (getHeight() - yp) - mStrokeWidth);
        mOval1 = new RectF(
                getWidth() / 2 - mStrokeWidth * .15f,
                getPaddingTop() + mStrokeWidth * .85f,
                getWidth() / 2 + mStrokeWidth * .15f,
                getPaddingTop() + mStrokeWidth * 1.15f);
        mOval2 = new RectF(
                getWidth() / 2 - mStrokeWidth * .25f,
                getPaddingTop() + mStrokeWidth * .75f,
                getWidth() / 2 + mStrokeWidth * .25f,
                getPaddingTop() + mStrokeWidth * 1.25f);
    }

    public void refreshTheLayout() {
        invalidate();
        requestLayout();
    }
}
